﻿component extends="ActiveDirectory" output="false"
{

	public Groups function init()
	{
        return this;
	}
    
    
    
    public any function setADGroup( required string ADGroupName = "" )
    {
        var GroupDN = this.SCFAD.getGroupDNByDisplayName(arguments.ADGroupName);        
        try{
            var attrs = this.SCFAD.getUserAttributesByDN(trim(GroupDN), 'member,groupType,memberOf,managedBy,description,objectClass,name');//works for groups
            Users = CreateObject("Component", 'Users').init();
            var GroupManagerAttrs = Users.setADUser( this.SCFAD.getAccountNameByDN(attrs.managedBy)).UserAttrs;
            var GuestGroup = getGuestGroup( arguments.ADGroupName );
            var ResourceGroup = getResourceGroup( arguments.ADGroupName );
            this.GroupAttrs = {
                member='#attrs.member#',groupType='#this.ADGroupTypes[attrs.groupType]#',memberOf='#attrs.memberOf#',
                managedBy='#attrs.managedBy#',description='#attrs.description#',objectClass='#attrs.objectClass#',
                name='#attrs.name#', GroupDN = '#GroupDN#', GroupManagerAttrs = GroupManagerAttrs, 
                GuestGroup = {
                    distinguishedName='#GuestGroup.distinguishedName[1]#',
                    sAMAccountName='#GuestGroup.sAMAccountName[1]#',
                    managedBy='#GuestGroup.managedBy[1]#',
                    member='#GuestGroup.member[1]#',
                    groupType='#GuestGroup.groupType[1]#'
                },
                ResourceGroup = {
                    distinguishedName='#ResourceGroup.distinguishedName[1]#',
                    sAMAccountName='#ResourceGroup.sAMAccountName[1]#',
                    managedBy='#ResourceGroup.managedBy[1]#',
                    member='#ResourceGroup.member[1]#',
                    groupType='#ResourceGroup.groupType[1]#'
                }
            };
        }
        catch(Any e){
            this.GroupAttrs = {
                member='[Not Found]',groupType='-42',memberOf='[Not Found]',
                managedBy='[Not Found]',description='[Not Found]',objectClass='[Not Found]',
                name='[Not Found]',GroupDN = '[Not Found]', 
                GroupManagerAttrs = {
                    distinguishedName='[Not Found]',sAMAccountName='[Not Found]',
                    title='[Not Found]',physicalDeliveryOfficeName='[Not Found]',department='[Not Found]',
                    manager='[Not Found]', displayName='[Not Found]',userPrincipalName='[Not Found]',memberOf='[Not Found]',
                    givenName='[Not Found]', initials='[Not Found]', sn='[Not Found]',directReports='[Not Found]',
                    lastLogoff='[Not Found]',lastLogon='[Not Found]',logonCount='[Not Found]', rosetta='[Not Found]', Badge='[Not Found]'
                },
                GuestGroup = {
                    distinguishedName='[Not Found]',sAMAccountName='[Not Found]',managedBy='[Not Found]',member='[Not Found]',groupType='-42'
                },
                ResourceGroup = {
                    distinguishedName='[Not Found]',sAMAccountName='[Not Found]',managedBy='[Not Found]',member='[Not Found]',groupType='-42'
                }
            };
        }
        return this;
    }
    
    
    
    public any function removeUserFromGroup( required string ADGroupDN, required string ADUserDN )
    {
        this.SCFAD.removeUserFromGroup(arguments.ADUserDN, arguments.ADGroupDN);
        return True;
    } 
    
    
    
    public any function getAllADGroups( )
    {
        return session.AllADGroups;
    }
    
    
    
    public query function getResourceGroup( required string SearchString )
    {
        GroupQuery = new query();
        GroupQuery.setAttributes(GetAllGroups = getAllADGroups());
        GroupQuery.addParam(name="guest_group_name",value="#trim(arguments.SearchString)# DataShare ReadWrite",cfsqltype="VARCHAR");
        GroupQuery.addParam(name="guest_group_name_other",value="#trim(arguments.SearchString)# Share DataShare ReadWrite",cfsqltype="VARCHAR");
        GroupBYDN = GroupQuery.execute(
            sql="SELECT * FROM GetAllGroups WHERE sAMAccountName = :guest_group_name OR sAMAccountName = :guest_group_name_other", 
            dbtype="query", maxrows=1);
        return GroupBYDN.getResult();
    }
    
    
    
    public query function getGuestGroup( required string SearchString )
    {
        GroupQuery = new query();
        GroupQuery.setAttributes(GetAllGroups = getAllADGroups());
        GroupQuery.addParam(name="guest_group_name",value="Guest #trim(arguments.SearchString)# DataShare",cfsqltype="VARCHAR");
        GroupQuery.addParam(name="guest_group_name_other",value="Guest #trim(arguments.SearchString)# Share DataShare",cfsqltype="VARCHAR");
        GroupBYDN = GroupQuery.execute(
            sql="SELECT * FROM GetAllGroups WHERE sAMAccountName = :guest_group_name OR sAMAccountName = :guest_group_name_other", 
            dbtype="query", maxrows=1);
        return GroupBYDN.getResult();
    }
    
    
    
    public query function getGroupByDN( required string SearchString )
    {
        GroupQuery = new query();
        GroupQuery.setAttributes(GetAllGroups = getAllADGroups());
        GroupQuery.addParam(name="groupname",value="%#Ucase( Left( arguments.SearchString,3) ) & Right( arguments.SearchString, len(arguments.SearchString)-3)#%",cfsqltype="VARCHAR");
        GroupQuery.addParam(name="groupnameLowerCase",value="%#Lcase(arguments.SearchString)#%",cfsqltype="VARCHAR");
        GroupQuery.addParam(name="groupnameAny",value="%#arguments.SearchString#%",cfsqltype="VARCHAR");
        GroupBYDN = GroupQuery.execute(
            sql="SELECT * FROM GetAllGroups WHERE sAMAccountName like :groupname OR 
                sAMAccountName like :groupnameLowerCase OR 
                sAMAccountName like :groupnameAny", 
            dbtype="query", maxRows="10");
        return GroupBYDN.getResult();
    }
    
    
    public query function getAllGroupTypes( )
    {
        GroupQuery = new query();
        GroupQuery.setAttributes(GetAllGroups = getAllADGroups());
        GroupType = GroupQuery.execute(
            sql="SELECT Count( GroupType ) AS GroupTypeCount FROM GetAllGroups GROUP BY GroupType ORDER BY GroupTypeCount DESC", 
            dbtype="query"
        );
        return GroupType.getResult();
    }
    
    
    public struct function getGroupMemberCount( )
    {
        GroupQuery = new query();
        GroupQuery.setAttributes(GetAllGroups = getAllADGroups());
        GroupMember = GroupQuery.execute(
            sql="SELECT sAMAccountName AS Name, member FROM GetAllGroups WHERE member <> ''", 
            dbtype="query"
        );
        GroupMemberStruct = {};
        for( i=1; i <= GroupMember.getResult().RecordCount; i++){
            GroupMemberStruct['#GroupMember.getResult().Name[i]#'] = '#ArrayLen(this.parseUserDNString(GroupMember.getResult().member[i]))#';
        }
        return GroupMemberStruct;
    }
    
    
    public query function getManagerGroupCount( )
    {
        GroupQuery = new query();
        GroupQuery.setAttributes(GetAllGroups = getAllADGroups());
        GroupManagerCount = GroupQuery.execute(
            sql="SELECT managedBy AS Manager, COUNT(sAMAccountName) AS GroupCount FROM GetAllGroups WHERE managedBy <> '' GROUP BY managedBy ORDER BY GroupCount DESC", 
            dbtype="query"
        );
        return GroupManagerCount.getResult();
    }

}